package cn.enilu.guns.business;

import cn.enilu.guns.bean.entity.door.EntranceGuardDoor;
import cn.enilu.guns.bean.entity.door.EntranceGuardRule;
import cn.enilu.guns.bean.vo.query.SearchFilter;
import cn.enilu.guns.service.door.EntranceGuardDoorService;
import cn.enilu.guns.service.door.EntranceGuardRuleService;
import cn.enilu.guns.utils.Lists;
import cn.enilu.guns.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DoorBusiness {

    @Autowired
    private EntranceGuardRuleService entranceGuardRuleService;
    @Autowired
    private EntranceGuardDoorService entranceGuardDoorService;
    /**
     * 获取门已设置的门禁权限
     */
     public List<EntranceGuardRule> getEntranceGuardRules(Long doorInfoId){
        //查询关系表，得到所有门对应的门禁权限
        List<SearchFilter> searchFilters = Lists.newArrayList();
        searchFilters.add(new SearchFilter("doorId",SearchFilter.Operator.EQ,doorInfoId));
        List<EntranceGuardDoor> entranceGuardDoors = entranceGuardDoorService.queryAll(searchFilters);
        //抽取List<EntranceGuardDoor>中的门禁权限ID列表
        List<Long> ruleIds = entranceGuardDoors.stream().map(EntranceGuardDoor::getRuleId).collect(Collectors.toList());

        //查询门禁权限的列表
         SearchFilter entranceGuardRuleSearchFilter = null;
         if(ruleIds.size() > 0) {
             entranceGuardRuleSearchFilter = new SearchFilter("id", SearchFilter.Operator.IN, ruleIds);
             List<EntranceGuardRule> entranceGuardRules = entranceGuardRuleService.queryAll(entranceGuardRuleSearchFilter, new Sort(Sort.Direction.ASC, "id"));
             return entranceGuardRules;

         }

        return Lists.newArrayList();
    }
}
